package com.pai4j.user.service.relation;


import com.pai4j.common.enums.AccountRelationTypeEnum;
import com.pai4j.common.enums.RoleCodeEnum;
import com.pai4j.common.exception.BusinessException;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class AccountRelationFactory implements InitializingBean, ApplicationContextAware {

    private ApplicationContext applicationContext;

   // 定义一个静态映射，用于存储角色与账户关系服务的映射关系
private static Map<Integer, AbstractAccountRelationService> services = new HashMap<>();

// 定义一个静态映射，用于存储关系类型与账户关系服务的映射关系
private static Map<Integer, AbstractAccountRelationService> serviceForType = new HashMap<>();

/**
 * 在所有属性设置完成后调用此方法以进行初始化
 * 主要负责注册服务到不同的映射中，以便根据角色或关系类型进行快速查找
 * @throws Exception 如果属性设置完成后的方法执行出现异常
 */
@Override
public void afterPropertiesSet() throws Exception {
    // 根据角色注册服务
    this.registryServiceByRole();
    // 根据关系类型注册服务
    this.registryServiceByRelationType();
}

    /**
     * 根据账号角色注册服务bean
     *
     */
    private void registryServiceByRole() {

        services.put(RoleCodeEnum.USER.getCode(), applicationContext.getBean(User2UserAccountRelationService.class));
        services.put(RoleCodeEnum.ADMIN.getCode(), applicationContext.getBean(User2UserAccountRelationService.class));
        services.put(RoleCodeEnum.ROBOT.getCode(), applicationContext.getBean(User2RobotAccountRelationService.class));
        services.put(RoleCodeEnum.GROUP.getCode(), applicationContext.getBean(User2GroupAccountRelationService.class));
        services.put(RoleCodeEnum.SUBJECT_ACCOUNT.getCode(), applicationContext.getBean(User2OfficialAccountRelationService.class));
        services.put(RoleCodeEnum.AIBOT.getCode(), applicationContext.getBean(User2AIAgentSenderAccountRelationService.class));
        services.put(RoleCodeEnum.SERVER_ACCOUNT.getCode(), applicationContext.getBean(User2ServerAccountRelationService.class));
    }

    /**
     * 根据关系类型注册服务bean
     */
    private void registryServiceByRelationType() {

        serviceForType.put(AccountRelationTypeEnum.USER_USER.getType(),
                applicationContext.getBean(User2UserAccountRelationService.class));
        serviceForType.put(AccountRelationTypeEnum.USER_GROUP.getType(),
                applicationContext.getBean(User2GroupAccountRelationService.class));
        serviceForType.put(AccountRelationTypeEnum.USER_SUBJECT.getType(),
                applicationContext.getBean(User2OfficialAccountRelationService.class));
        serviceForType.put(AccountRelationTypeEnum.USER_ROBOT.getType(),
                applicationContext.getBean(User2RobotAccountRelationService.class));
        serviceForType.put(AccountRelationTypeEnum.USER_SERVER.getType(),
                applicationContext.getBean(User2ServerAccountRelationService.class));
    }

    public static AbstractAccountRelationService getServiceByCode(Integer roleCode) {
        AbstractAccountRelationService accountRelationService = services.get(roleCode);
        if (accountRelationService != null) {
            return accountRelationService;
        }
        throw new BusinessException("不支持的关系服务");
    }

    public static AbstractAccountRelationService getServiceByType(Integer relationType) {
        AbstractAccountRelationService accountRelationService = serviceForType.get(relationType);
        if (accountRelationService != null) {
            return accountRelationService;
        }
        throw new BusinessException("不支持的关系服务");
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}
